package api

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"seat-service/model"
	"seat-service/model/dto"
	"seat-service/response"
	service "seat-service/service/impl"
	"strconv"
)

var dictionaryService service.DictionaryService

type DictionaryApi struct {
}

func (*DictionaryApi) AddMaster(c *gin.Context) {
	var addDto dto.AddDictionaryDto
	err := c.ShouldBindJSON(&addDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}

	err = dictionaryService.AddDictionaryMaster(model.Dictionary{
		Name:             addDto.Name,
		Code:             addDto.Code,
		DictionaryStatus: "1",
	})
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}

func (*DictionaryApi) DeleteMaster(c *gin.Context) {
	id := c.Query("id")
	numInt, _ := strconv.Atoi(id)
	err := dictionaryService.DeleteDictionaryMaster(uint(numInt))
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}

	response.Success(c, response.CodeSuccess, nil)
}

func (*DictionaryApi) BatchDeleteMaster(c *gin.Context) {
	var batchDto dto.BatchDeleteMasterDto
	err := c.ShouldBindJSON(&batchDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}

	err = dictionaryService.BatchDeleteDictionaryMaster(batchDto.IDSlice...)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}

	response.Success(c, response.CodeSuccess, nil)
}

func (*DictionaryApi) UpdateMaster(c *gin.Context) {
	var MasterDto dto.UpdateMasterDto
	err := c.ShouldBindJSON(&MasterDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	numInt, _ := strconv.Atoi(MasterDto.ID)
	err = dictionaryService.UpdateDictionaryMaster(model.Dictionary{
		Model:            gorm.Model{ID: uint(numInt)},
		Name:             MasterDto.Name,
		Code:             MasterDto.Code,
		DictionaryStatus: MasterDto.DictionaryStatus,
	})
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}

func (*DictionaryApi) QueryMaster(c *gin.Context) {
	page := c.Query("page")
	pageSize := c.Query("page_size")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	res, err := dictionaryService.QueryDictionaryMaster(p, ps)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, res)
}
func (*DictionaryApi) QueryMasterByType(c *gin.Context) {
	page := c.Query("page")
	pageSize := c.Query("page_size")
	condition := c.Query("condition")
	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	res, err := dictionaryService.QueryDictionaryMasterByType(condition, p, ps)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, res)
}

func (*DictionaryApi) AddSlave(c *gin.Context) {
	var addDto dto.AddDictionarySlaveDto
	err := c.ShouldBindJSON(&addDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	slaveId, _ := strconv.Atoi(addDto.DictionaryId)
	value, _ := strconv.Atoi(addDto.Value)
	err = dictionaryService.AddDictionarySlave(model.DictionarySlave{
		Name:                  addDto.Name,
		Value:                 value,
		DictionarySlaveStatus: "1",
		DictionaryId:          uint(slaveId),
	})
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}
func (*DictionaryApi) DeleteSlave(c *gin.Context) {
	id := c.Query("id")
	numInt, _ := strconv.Atoi(id)
	err := dictionaryService.DeleteDictionarySlave(uint(numInt))
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}

	response.Success(c, response.CodeSuccess, nil)
}
func (*DictionaryApi) BatchDeleteSlave(c *gin.Context) {
	var batchDto dto.BatchDeleteMasterDto
	err := c.ShouldBindJSON(&batchDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}

	err = dictionaryService.BatchDeleteDictionarySlave(batchDto.IDSlice...)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}

	response.Success(c, response.CodeSuccess, nil)
}
func (*DictionaryApi) UpdateSlave(c *gin.Context) {
	var MasterDto dto.UpdateSlaveDto
	err := c.ShouldBindJSON(&MasterDto)
	if err != nil {
		response.Fail(c, response.DataInWrongFormat, nil)
		return
	}
	numInt, _ := strconv.Atoi(MasterDto.ID)
	value, _ := strconv.Atoi(MasterDto.Value)
	err = dictionaryService.UpdateDictionarySlave(model.DictionarySlave{
		Model:                 gorm.Model{ID: uint(numInt)},
		Name:                  MasterDto.Name,
		Value:                 value,
		DictionarySlaveStatus: MasterDto.DictionarySlaveStatus,
	})
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, nil)
}
func (*DictionaryApi) QuerySlave(c *gin.Context) {
	page := c.Query("page")
	pageSize := c.Query("page_size")
	id := c.Query("dictionary_id")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	dictionaryId, _ := strconv.Atoi(id)

	res, err := dictionaryService.QueryDictionarySlave(p, ps, dictionaryId)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, res)
}
func (*DictionaryApi) QuerySlaveByName(c *gin.Context) {
	page := c.Query("page")
	pageSize := c.Query("page_size")
	condition := c.Query("condition")
	id := c.Query("dictionary_id")
	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	dictionaryId, _ := strconv.Atoi(id)
	res, err := dictionaryService.QueryDictionarySlaveByType(condition, p, ps, dictionaryId)
	if err != nil {
		response.Fail(c, response.DatabaseException, nil)
		return
	}
	response.Success(c, response.CodeSuccess, res)
}
